package com.peak.distribution.jms.agent;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.peak.distribution.entity.agent.DisAgent;
import com.peak.distribution.manager.agent.DisAgentManager;
import com.peak.spring.boot.core.mapper.JsonMapper;
import com.peak.spring.boot.tools.Converts;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@Transactional
public class AgentConsumer {
	
	@Autowired
	private DisAgentManager agentManager;

	@JmsListener(destination = "${mq.queue.add.sub.agent.count}")
	public void receiveChangeStatusQueue(String json) {
		log.info("增加下级代理啦：{}", json);
		Map<String, Object> map = JsonMapper.nonDefaultMapper().fromJson(json, HashMap.class, String.class, Object.class);
		// 获取订单id和变更的订单状态
		Integer agentId = Converts.toInteger(map.get("agentId"));
		
		DisAgent agent = agentManager.getOne(agentId);
		agent.setSubAgentCount(Converts.toInt(agent.getSubAgentCount()) + 1);
		agentManager.save(agent);
	}
}
